package bing.commons.xmloperator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/*
 * 读取.classpath文件，解析path字段，然后修改某个字段的值。
 */
public class ReadXmlAndMod {

	private static List<String> classpathfilelist=new ArrayList();
	public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
		// TODO Auto-generated method stub
		String rootpath="";//根目录
		findclasspathFile(rootpath);
		
		int filecount=classpathfilelist.size();
		for(int i=0;i<filecount;i++){
			parseXmlAndMod(classpathfilelist.get(i));
		}
	}
	/*
	 * 找出所有的classpath文件，并记录在List<String> classpathfilelist变量中。
	 */
	public static void findclasspathFile(String fileName){
        File file = new File(fileName);  
        if (file.isDirectory()) {  
            File[] files = file.listFiles();  
            for (int i = 0; i < files.length; i++) {  
                String name = files[i].getName();  
                if (name.trim().toLowerCase().endsWith(".classpath")) {  
                    System.out.println(files[i].getPath() + "\t");  
                    classpathfilelist.add(files[i].getPath());//添加到list中
                }  
                if (files[i].isDirectory()) {  
                    String path = files[i].getPath();  
                    findclasspathFile(path);  
                }  
            }  
        }  
	}
	public static void parseXmlAndMod(String filepath) throws DocumentException, FileNotFoundException, IOException{
		SAXReader reader = new SAXReader();
		  Document document = reader.read(new File(filepath));
		  Element root = document.getRootElement();

		  Iterator it = root.elementIterator();
		  while (it.hasNext()) {
		   Element element = (Element) it.next();

		   //未知属性名称情况下
		   /*Iterator attrIt = element.attributeIterator();
		   while (attrIt.hasNext()) {
		    Attribute a  = (Attribute) attrIt.next();
		    System.out.println(a.getValue());
		   }*/

		   //已知属性名称情况下
		   if(element.attributeValue("path").endsWith(".jar")){
		   System.out.println("path: " + element.attributeValue("path"));
		   //element.attributeValue("path").g
		   File tempFile=new File(element.attributeValue("path"));
		   element.attribute("path").setValue(""+tempFile.getName());//!!!!!!!!!!!!!!!!!设置更改的值
		   }
		   //未知元素名情况下
		   /*Iterator eleIt = element.elementIterator();
		   while (eleIt.hasNext()) {
		    Element e = (Element) eleIt.next();
		    System.out.println(e.getName() + ": " + e.getText());
		   }
		   System.out.println();*/

		   //已知元素名情况下
//		   System.out.println("title: " + element.elementText("title"));
//		   System.out.println("author: " + element.elementText("author"));
//		   System.out.println();
		  }
	        FileOutputStream output = new FileOutputStream(new File(filepath));  
	        XMLWriter writer = new XMLWriter(output);  
	        writer.write(document);  
	        writer.flush();  
	        writer.close();  
	}
}
